load("//bazel:mongo_src_rules.bzl", "idl_generator", "mongo_cc_fuzzer_test", "mongo_cc_integration_test", "mongo_cc_library", "mongo_cc_unit_test")

package(default_visibility = ["//visibility:public"])

exports_files(
    glob([
        "*.h",
        "*.cpp",
    ]),
)

mongo_cc_library(
    name = "message",
    srcs = [
        "message.cpp",
        "op_msg.cpp",
    ],
    deps = [
        "//src/mongo/bson:bson_validate",
        "//src/mongo/bson/dotted_path:dotted_path_support",
        "//src/mongo/bson/util:bson_extract",
        "//src/mongo/db:multitenancy",
        "//src/mongo/db:server_base",
        "//src/mongo/db:wire_version",
        "//src/mongo/db/auth:security_token_auth",
    ] + select({
        "//bazel/config:use_wiredtiger_enabled": [
            "//src/third_party/wiredtiger:wiredtiger_checksum",
        ],
        "//conditions:default": [],
    }),
)

idl_generator(
    name = "topology_version_gen",
    src = "topology_version.idl",
    deps = [
        "//src/mongo/db:basic_types_gen",
    ],
)

mongo_cc_library(
    name = "client_metadata",
    srcs = [
        "//src/mongo/rpc/metadata:client_metadata.cpp",
        "//src/mongo/rpc/metadata:client_metadata_gen",
    ],
    deps = [
        "//src/mongo:base",
        "//src/mongo/db:server_base",
        "//src/mongo/db:service_context",
        "//src/mongo/transport:message_compressor",
        "//src/mongo/util:processinfo",
        "//src/mongo/util/concurrency:spin_lock",
        "//src/mongo/util/net:network",
    ],
)

mongo_cc_library(
    name = "metadata",
    srcs = [
        "metadata.cpp",
        "topology_version_gen",
        "//src/mongo/rpc/metadata:egress_metadata_hook_list.cpp",
        "//src/mongo/rpc/metadata:oplog_query_metadata.cpp",
        "//src/mongo/rpc/metadata:repl_set_metadata.cpp",
    ],
    deps = [
        ":client_metadata",
        ":message",
        ":metadata_impersonated_user",
        "//src/mongo/bson/util:bson_extract",
        "//src/mongo/client:read_preference",
        "//src/mongo/db:multitenancy",
        "//src/mongo/db:server_base",
        "//src/mongo/db:signed_logical_time",
        "//src/mongo/db:vector_clock",
        "//src/mongo/db/auth:security_token",
        "//src/mongo/db/repl:optime",
        "//src/mongo/db/shard_role/shard_catalog:raw_data_operation",
        "//src/mongo/db/topology/user_write_block:write_block_bypass",
        "//src/mongo/util/concurrency:spin_lock",
    ],
)

idl_generator(
    name = "object_check_gen",
    src = "object_check.idl",
)

mongo_cc_library(
    name = "rpc",
    srcs = [
        "check_allowed_op_query_cmd.cpp",
        "factory.cpp",
        "legacy_reply.cpp",
        "legacy_reply_builder.cpp",
        "legacy_request.cpp",
        "object_check.cpp",
        "reply_builder_interface.cpp",
        ":object_check_gen",
    ],
    deps = [
        ":message",
        ":metadata",
        ":rewrite_state_change_errors",
        "//src/mongo/db:dbmessage",
        "//src/mongo/db:server_base",
        "//src/mongo/db/commands:test_commands_enabled",
        "//src/mongo/db/stats:counters",
        "//src/mongo/s:common_s",
        "//src/mongo/util/net:network",
    ],
)

mongo_cc_library(
    name = "metadata_impersonated_user",
    srcs = [
        "//src/mongo/rpc/metadata:audit_attrs_gen",
        "//src/mongo/rpc/metadata:audit_client_attrs.cpp",
        "//src/mongo/rpc/metadata:audit_metadata.cpp",
        "//src/mongo/rpc/metadata:audit_user_attrs.cpp",
        "//src/mongo/rpc/metadata:audit_user_attrs_client_observer.cpp",
        "//src/mongo/rpc/metadata:impersonated_client_session.cpp",
    ],
    deps = [
        "//src/mongo:base",
        "//src/mongo/db:server_base",
        "//src/mongo/db:server_feature_flags",  # TODO(SERVER-83990): Remove.
        "//src/mongo/db:server_options",
        "//src/mongo/db:service_context",
        "//src/mongo/db/auth",
    ],
)

idl_generator(
    name = "write_concern_error_gen",
    src = "write_concern_error.idl",
    deps = [
        "//src/mongo/db:basic_types_gen",
    ],
)

idl_generator(
    name = "rewrite_state_change_errors_server_parameter_gen",
    src = "rewrite_state_change_errors_server_parameter.idl",
)

mongo_cc_library(
    name = "rewrite_state_change_errors",
    srcs = [
        "rewrite_state_change_errors.cpp",
        "rewrite_state_change_errors_server_parameter_gen",
    ],
    deps = [
        ":message",
        "//src/mongo:base",
        "//src/mongo/bson/util:bson_extract",
        "//src/mongo/db:server_base",
        "//src/mongo/db:service_context",
        "//src/mongo/db/exec/mutable_bson",
        "//src/mongo/util:pcre_wrapper",
    ],
)

mongo_cc_library(
    name = "command_status",
    srcs = [
        "get_status_from_command_result_write_util.cpp",
        "write_concern_error_detail.cpp",
        "write_concern_error_gen",
    ],
    deps = [
        "//src/mongo/bson/util:bson_extract",
        "//src/mongo/db:common",
        "//src/mongo/db:server_base",
    ],
)

mongo_cc_library(
    name = "get_status_from_command_result",
    srcs = [
        "get_status_from_command_result.cpp",
    ],
    deps = [
        "//src/mongo:base",
    ],
)

mongo_cc_integration_test(
    name = "rpc_integration_test",
    srcs = [
        "op_legacy_integration_test.cpp",
        "op_msg_integration_test.cpp",
    ],
    deps = [
        ":message",
        "//src/mongo/client:clientdriver_network",
        "//src/mongo/transport:transport_layer_egress_init",
        "//src/mongo/util:version_impl",
    ],
)

mongo_cc_unit_test(
    name = "rpc_test",
    srcs = [
        "get_status_from_command_result_test.cpp",
        "metadata_test.cpp",
        "object_check_test.cpp",
        "op_msg_test.cpp",
        "reply_builder_test.cpp",
        "rewrite_state_change_errors_test.cpp",
        "//src/mongo/rpc/metadata:audit_metadata_test.cpp",
        "//src/mongo/rpc/metadata:client_metadata_test.cpp",
        "//src/mongo/rpc/metadata:egress_metadata_hook_list_test.cpp",
        "//src/mongo/rpc/metadata:oplog_query_metadata_test.cpp",
        "//src/mongo/rpc/metadata:repl_set_metadata_test.cpp",
        "//src/mongo/rpc/metadata:security_token_metadata_test.cpp",
    ],
    tags = ["mongo_unittest_fifth_group"],
    target_compatible_with = select({
        "//bazel/config:use_wiredtiger_enabled": [],
        "//conditions:default": ["@platforms//:incompatible"],
    }),
    deps = [
        ":client_metadata",
        ":metadata",
        ":rewrite_state_change_errors",
        ":rpc",
        "//src/mongo/client:clientdriver_minimal",
        "//src/mongo/db:service_context_test_fixture",
        "//src/mongo/db/auth:authmocks",
        "//src/mongo/db/shard_role:service_context_non_d",
        "//src/mongo/transport:transport_layer_mock",
        "//src/third_party/wiredtiger:wiredtiger_checksum",
    ],
)

mongo_cc_unit_test(
    name = "rpc_d_test",
    srcs = [
        "//src/mongo/rpc/metadata:audit_user_attrs_client_observer_test.cpp",
    ],
    tags = [
        "mongo_unittest_first_group",
    ],
    deps = [
        ":client_metadata",
        ":metadata",
        ":rpc",
        "//src/mongo/client:clientdriver_minimal",
        "//src/mongo/db:service_context_d",
        "//src/mongo/db:service_context_d_test_fixture",
        "//src/mongo/db/auth:authorization_session_test_fixture",
    ],
)

# TODO(SERVER-96852): Remove cycle created by moving //src/mongo/rpc:message.h to //src/mongo/rpc:message
filegroup(
    name = "message_hdrs",
    srcs = [":message.h"],
)

# TODO(SERVER-96852): Remove cycle created by moving //src/mongo/rpc:op_msg.h to //src/mongo/rpc:message
filegroup(
    name = "op_msg_hdrs",
    srcs = [":op_msg.h"],
)

mongo_cc_fuzzer_test(
    name = "protocol_fuzzer",
    srcs = [
        "protocol_fuzzer.cpp",
    ],
    deps = [
        "message",
        "rpc",
        "//src/mongo/db/query/write_ops:write_ops_parsers",
        "//src/mongo/db/shard_role/lock_manager",
        "//src/mongo/transport:message_compressor",
    ],
)
